import time

from tools import ImageUtil, ActionUtil, WindowUtil


class RepairIn:
    """
    关卡内检查修理装备
    """

    def __init__(self, position_grab):

        self.position_grab = position_grab

        self.to_repair_img = "images/to_repair_in.bmp"

        self.repair_all_img = "images/repair_all.bmp"

        self.close_img = "images/close.bmp"

    def repair_main(self):
        # 修装备
        window = WindowUtil.grab_screen(self.position_grab)
        flag, loc = ImageUtil.is_icon_exist(window, self.to_repair_img, (25, 25))
        print(f"[to_repair]: {flag} {loc}")
        if flag:
            ActionUtil.mouse_left_click((loc[0], loc[1]))
            time.sleep(2)
        else:
            # 未找到修理装备图标，则开始战斗
            return True, 1

        window = WindowUtil.grab_screen(self.position_grab)
        flag, loc = ImageUtil.is_icon_exist(window, self.repair_all_img, (60, 15))
        print(f"[repair_all]: {flag} {loc}")
        if flag:
            ActionUtil.mouse_left_click((loc[0], loc[1]))
            time.sleep(2)

        flag, loc = ImageUtil.is_icon_exist(window, self.close_img, (20, 20))
        print(f"[close]: {flag} {loc}")
        if flag:
            ActionUtil.mouse_left_click((loc[0], loc[1]))
            time.sleep(2)

        return True, 1
